package com.woniu.atms.controller.aft;

import com.github.pagehelper.PageInfo;
import com.woniu.atms.entity.G5Route;
import com.woniu.atms.entity.G5Site;
import com.woniu.atms.mapper.SiteMapper;
import com.woniu.atms.service.RouteService;
import com.woniu.atms.service.SiteService;
import com.woniu.atms.util.Code;
import com.woniu.atms.util.ResponseResult;
import com.woniu.atms.vo.aft.route.FindRouteByStatusVO;
import com.woniu.atms.vo.aft.route.RouteAddVO;
import com.woniu.atms.vo.aft.route.RoutePageVO;
import com.woniu.atms.vo.aft.route.SEAddress;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lhy
 * @title: AftRouteController 线路管理
 * @projectName g5_atms
 * @description: TODO
 * @date 2022/1/17 15:17
 */
@Slf4j
@RestController
@RequestMapping("/api/aft/v1/route")
public class AftRouteController {
    //线路列表(/list);查询线路(/info_id);根据起止点查询线路(/list_site);
    //根据路线状态查询线路(/list_status);设置线路是否可用(/update);

    @Autowired
    private RouteService routeService;
    @Resource
    private SiteMapper siteMapper;
    @Autowired
    private SiteService siteService;

    @RequestMapping("/list")
    public ResponseResult findAllRoute(@RequestBody RoutePageVO routePageVO) {
        if (routePageVO.getPageNum() == null) {//判断页码是否为空
            routePageVO.setPageNum(1);
        }
        PageInfo<G5Route> pageInfo = routeService.findAllRoute(routePageVO.getPageNum());
        return new ResponseResult(pageInfo);
    }

    @RequestMapping("/info_id/{id}")
    public ResponseResult findRouteById(@PathVariable int id) {
        G5Route route = routeService.findRouteById(id);
        return new ResponseResult(route);
    }

    /**
     * 根据起止站点所属地区查询站点
     *
     * @param seAddress
     * @return
     */
    @PostMapping("/list_site")
    public ResponseResult findRoutesByStarAndEndAddress(@RequestBody @Valid SEAddress seAddress) {
        List<G5Site> startSites = siteService.findSiteByFuzzyAddress(seAddress.getStartAddress());
        List<G5Site> endSites = siteService.findSiteByFuzzyAddress(seAddress.getEndAddress());
        if (startSites.size() == 0) {
            return new ResponseResult(Code.FAIL, "起始站点地区错误！", null);
        }
        if (endSites.size() == 0) {
            return new ResponseResult(Code.FAIL, "目的地站点地区错误！", null);
        }
        List<G5Route> routes = routeService.findRoutesByStarAndEndAddress(startSites, endSites);
        return new ResponseResult(Code.SUCCESS, "查询成功", routes);
    }

    @PostMapping("/list_status")
    public ResponseResult findRouteByStatus(@RequestBody FindRouteByStatusVO findRouteByStatusVO) {
        Integer pageNum = findRouteByStatusVO.getPageNum();
        if (pageNum == null) {//判断页码是否为空
            pageNum = 1;
        }
        return new ResponseResult(routeService.findRouteByStatus(findRouteByStatusVO.getStatus(), pageNum));
    }

    /**
     * 修改线路状态或删除路线
     *
     * @param route
     * @return
     */
    @PostMapping("/update")
    public ResponseResult updateRoute(@RequestBody G5Route route) {
        boolean flag = routeService.updateRoute(route);
        if (flag) {
            return new ResponseResult(Code.SUCCESS, "修改成功！", null);
        } else {
            return new ResponseResult(Code.FAIL, "修改失败！", null);
        }
    }

    @PostMapping("/add")
    public ResponseResult addRoute(@RequestBody @Valid RouteAddVO routeAddVO) {
        //判断路线名是否存在
        G5Route route1 = routeService.findRouteByName(routeAddVO.getRouteName());
        if (route1 != null) {
            return new ResponseResult(Code.FAIL, "路线名已经存在！", null);
        }
        //判断起始站和终点站不能相同
        if (routeAddVO.getStartSiteName().equals(routeAddVO.getEndSiteName())) {
            return new ResponseResult(Code.FAIL, "起始站和终点站不能相同！", null);
        }
        //判断要添加的起始站点和终点站点这条路线不存在
        G5Site startSite = siteMapper.findSiteByName(routeAddVO.getStartSiteName());//获取起点站数据
        G5Site endSite = siteMapper.findSiteByName(routeAddVO.getEndSiteName());//获取终点站数据
        List<G5Site> startSites = new ArrayList<>();
        List<G5Site> endSites = new ArrayList<>();
        startSites.add(startSite);
        endSites.add(endSite);
        //根据起点站和终点站查询线路
        List<G5Route> routes = routeService.findRoutesByStarAndEndAddress(startSites, endSites);
        if (routes.size() != 0) {
            return new ResponseResult(Code.FAIL, "该起始站点之间已经存在路线！", null);
        }

        //校验无误后添加数据
        G5Route route = new G5Route();
        route.setRouteName(routeAddVO.getRouteName());
        route.setSpendTime(routeAddVO.getSpendTime());
        route.setStartSite(startSite);
        route.setEndSite(endSite);
        boolean flag = routeService.addRoute(route);
        if (flag) {
            return new ResponseResult(Code.SUCCESS, "添加成功！", null);
        } else {
            return new ResponseResult(Code.FAIL, "添加失败！", null);
        }
    }

}
